//+------------------------------------------------------------------+
//|                                                          bbi.mq4 |
//|                                                    Dimitri Rudel |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Dimitri Rudel"
#property link      ""

#include <basic.mqh>
#include <tradeManager.mqh>


extern double exRsiLow = 30;
extern double exRsiHigh = 70;
extern double exRsiLong = 35;
extern double exRsiShort = 65;

extern double exAdx = 14;
extern double exAdxMax = 27;



extern double exRsi = 14;
extern double exBb = 50;
extern double exBbDeviation = 4;//50

extern int exCntCandleRsiIn = 1;
extern int exCntCandleRsiOut = 2;

extern int exEntrySlPip = 15;
extern int exTakeProfitPip = 14;

//trailStop
extern string TRAILSTOP = "TRAILSTOP";
extern int exTrailStop = 1;
extern int exCntCandlesAboveBbMid = 4;
extern int exPipTrailSl = 15;


double bbLower = 0;
double bbMid = 0;
double bbUpper = 0;
double adxMain = 0;



int oldTime;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
      b_debug("BBI INIT");
      oldTime = Time[0];
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
      b_debug("BBI DEINIT");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
     if(oldTime == Time[0] ){
         return(0);   
      }
     if(Bars < exBb){
         return(0);
     }
     oldTime = Time[0];
     
      bbLower = iBands(Symbol(),0,exBb,exBbDeviation,0,PRICE_CLOSE,MODE_LOWER,1); 
      bbUpper = iBands(Symbol(),0,exBb,exBbDeviation,0,PRICE_CLOSE,MODE_UPPER,1);  
      bbMid = (bbLower + bbUpper)/2;
      adxMain = iADX(Symbol(),0,exAdx,PRICE_CLOSE,MODE_MAIN,1);
      tmgr_checkExit();
      
      if(exTrailStop == 1){
         trailStop();
      }
      if(!b_isTradeTime()){
         return(0);
      }

      if(adxMain < exAdxMax ){
         return (0);
      }
      
      
         
      if(iRSI(Symbol(),0,exRsi,PRICE_CLOSE,1) > exRsiShort){
         
         bool cntCandleClose = true;
         for(int i = 0; i <= exCntCandleRsiOut; i++){
            if(iRSI(Symbol(),0,exRsi,PRICE_CLOSE,i) < exRsiHigh){        
            }else{
               cntCandleClose = false;
               break;
            }
            
            if(cntCandleClose){
               
               if(iRSI(Symbol(),0,exRsi,PRICE_CLOSE,cntCandleClose+1) > exRsiHigh){
                  double tp = Bid - b_getPriceByPip(exTakeProfitPip);
                  //iBands(Symbol(),0,exBb,exRsiDeviation,0,PRICE_CLOSE
                  double sl = bbUpper + b_getPriceByPip(exEntrySlPip);
                  b_OrderSend(Symbol(),1,0.02,Bid,3,sl,tp);
               }   
            }
         }
         
      }
      
      if(iRSI(Symbol(),0,exRsi,PRICE_CLOSE,1) > exRsiLong){
         
         cntCandleClose = true;
         for(i = 0; i <= exCntCandleRsiOut; i++){
            if(iRSI(Symbol(),0,exRsi,PRICE_CLOSE,i) > exRsiLow){        
            }else{
               cntCandleClose = false;
               break;
            }
            
            if(cntCandleClose){
               
               if(iRSI(Symbol(),0,exRsi,PRICE_CLOSE,cntCandleClose+1) < exRsiLow){
                  tp = Ask + b_getPriceByPip(exTakeProfitPip);
                  //iBands(Symbol(),0,exBb,exRsiDeviation,0,PRICE_CLOSE
                  sl = bbLower - b_getPriceByPip(exEntrySlPip);
                  b_OrderSend(Symbol(),0,0.02,Ask,3,sl,tp);
               }   
            }
         }
         
      }
      
//----
   return(0);
  }
//+------------------------------------------------------------------+

void trailStop(){
   for(int i=0; i < OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         double stoploss = OrderStopLoss();
         if ( OrderSymbol()==Symbol()) {
           //Candles closes over the mid band
           
           bool trail = true;
           if(OrderType() == 0){
               for(int j = 1; j <= exCntCandlesAboveBbMid; j++){
                  if(Close[j] < bbMid){
                     trail = false;
                     break;
                  } 
               }
               if(trail){
                  if(stoploss < bbMid - b_getPriceByPip(exPipTrailSl)){
                     b_OrderModify(OrderTicket(),OrderOpenPrice(),bbMid - b_getPriceByPip(exPipTrailSl),OrderTakeProfit(),0,Blue); 
                  }   
               }
           }else{
               for(int k = 1; k <= exCntCandlesAboveBbMid; k++){
                  if(Close[k] > bbMid){
                     trail = false;
                     break;
                  } 
               }
               if(trail){
                  if(stoploss > bbMid + b_getPriceByPip(exPipTrailSl)){
                     b_OrderModify(OrderTicket(),OrderOpenPrice(),bbMid + b_getPriceByPip(exPipTrailSl),OrderTakeProfit(),0,Blue);
                  }    
               }
           
           }
              
         }
      }
   }
}